package com.njupt.user.webapp.action;

import javax.mail.MessagingException;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.njupt.service.BaseAction;
import com.njupt.user.model.User;
import com.opensymphony.xwork2.Preparable;
@ParentPackage(value="user")
@Results({
	@Result(name = "view", location = "/profile/profile_view.jsp")
})
public class UserAction extends BaseAction implements Preparable
{
	private static final long serialVersionUID = 1L;
	private User user;
	private String succ;
	private String err;
	private String type;
	@Override
	public void prepare() throws Exception
	{
		Long userId = (Long) this.getParameter("user.id", Long.class);
		if(userId!=null){
			user = (User) manager.get(User.class, userId);
		}
	}
	public String save() throws MessagingException{
		String old_password = (String) this.getParameter("old_password", String.class);
		if(old_password!=null&&!old_password.equals(user.getPassword())){ 
			err = "你的旧密码输入不对";
			return VIEW;
		}
		String old_email = (String) this.getParameter("old_email", String.class);
		if(old_email!=null&&!old_email.equals(user.getEmail())) {
			err="你的旧邮箱地址输入不对";
			return VIEW;
		}
		String new_password = (String) this.getParameter("new_password", String.class);
		if(new_password!=null){
			user.setPassword(new_password);
		}
		String new_email = (String) this.getParameter("new_email", String.class);
		if(new_email!=null){
			user.setEmail(new_email);
			user.setUsername(new_email);
			user.setEnabled(false);
			new RegisterAction().sendMail(user);
		}
		succ = "您的设置已经保存";
		user = (User) manager.save(user);
		return VIEW;
	}
	public User getUser()
	{
		return user;
	}

	public void setUser(User user)
	{
		this.user = user;
	}
	public String getSucc()
	{
		return succ;
	}
	public void setSucc(String succ)
	{
		this.succ = succ;
	}
	public String getErr()
	{
		return err;
	}
	public void setErr(String err)
	{
		this.err = err;
	}
	public String getType()
	{
		return type;
	}
	public void setType(String type)
	{
		this.type = type;
	}

}
